package hust.music.util;




import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class FileHash {
	
		public static final char[] hexChar = { '0', '1', '2', '3', '4', '5', '6',
		'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		// public static final String[] hashTypes = new String[] { "MD2", "MD5",
		// "SHA1", "SHA-384", "SHA-512" };
		// public static final String[] hashTypes = new String[] { "MD5","SHA1",
		// "SHA-384", "SHA-512" };7 a; ]$ g: P; n2 r  f
		public static final String[] hashTypes = new String[] { "MD5" };
		/*public static void main(String[] args) {
			String fileName = "C:\\atlog.txt";
			System.out.println("" + fileName);
			System.out.println(getHash(fileName));
		}*/
		public static String getHash(String fileName) {
				List<MessageDigest> mds = new ArrayList<MessageDigest>();
				try {
					for (int i = 0; i < hashTypes.length; i++) {
						MessageDigest md = MessageDigest.getInstance(hashTypes[i]);
						mds.add(md);
					}
				} catch (NoSuchAlgorithmException e) {
					e.printStackTrace();
				}
				InputStream fis = null;
				try {
					fis = new FileInputStream(fileName);
				
					byte[] buffer = new byte[1024];
					int numRead = 0;
					MessageDigest md = null;
					for (int i = 0; i < mds.size(); i++) {
						md = (MessageDigest) mds.get(i);
						while ((numRead = fis.read(buffer)) > 0) {
							md.update(buffer, 0, numRead);
						}
					}
					} catch (Exception ex) {
						ex.printStackTrace();
					} finally {
						if (fis != null) {
							try {
								fis.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
					MessageDigest md = null;
					StringBuffer hash = new StringBuffer("");
					for (int i = 0; i < mds.size(); i++) {
						md = (MessageDigest) mds.get(i);
						hash.append(toHexString(md.digest()));
					}
					return hash.toString();
			}
		
		public static String getHash(File file){
			List<MessageDigest> mds = new ArrayList<MessageDigest>();
			try {
				for (int i = 0; i < hashTypes.length; i++) {
					MessageDigest md = MessageDigest.getInstance(hashTypes[i]);
					mds.add(md);
				}
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
			InputStream fis = null;
			try {
				fis = new FileInputStream(file);
				byte[] buffer = new byte[1024];
				int numRead = 0;
				MessageDigest md = null;
				for (int i = 0; i < mds.size(); i++) {
					md = (MessageDigest) mds.get(i);
					while ((numRead = fis.read(buffer)) > 0) {
						md.update(buffer, 0, numRead);
					}
				}
				} catch (Exception ex) {
					ex.printStackTrace();
				} finally {
					if (fis != null) {
						try {
							fis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				MessageDigest md = null;
				StringBuffer hash = new StringBuffer("");
				for (int i = 0; i < mds.size(); i++) {
					md = (MessageDigest) mds.get(i);
					hash.append(toHexString(md.digest()));
				}
				return hash.toString();
		}
			private static String toHexString(byte[] b) {
				StringBuffer sb = new StringBuffer(b.length * 2);
				for (int i = 0; i < b.length; i++) {
					sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
				}
				return sb.toString();	
			}
			
			public static void main(String[] args){
				System.out.println(getHash(new File("E:\\DMDownLoad\\Song\\林俊杰-一眼万年.mp3")));
				System.out.print(getHash(new File("E:\\DMDownLoad\\Song\\陈奕迅-爱情转移.mp3")));
				System.out.print(getHash(new File("E:\\DMDownLoad\\Song\\陈奕迅-爱情转移.mp3")));
			}
}
